﻿using System;
using System.Linq;
using Bots.DungeonBuddy.Attributes;
using Bots.DungeonBuddy.Helpers;
using CommonBehaviors.Actions;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.POI;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using TreeSharp;
using Styx;
using Styx.WoWInternals.WoWObjects;
using System.Collections.Generic;
using Action = TreeSharp.Action;

namespace Bots.DungeonBuddy.Dungeons.Classic
{
    public class TheDeadmines : Dungeon
    {
        #region Overrides of Dungeon

        /// <summary> The mapid of this dungeon. </summary>
        /// <value>The map identifier.</value>
        public override uint DungeonId
        {
            get { return 6; }
        }

        #endregion

        public override void WeighTargetsFilter(List<Targeting.TargetPriority> units)
        {
            foreach (var t in units)
            {
                var prioObject = t.Object;

                //Target good ones first
                if (prioObject.Entry == 48418 || prioObject.Entry == 11318)
                {
                    t.Score += 400;
                }
            }
        }

        #region Encounter Handlers
        
        [EncounterHandler(47403, Mode = CallBehaviorMode.Proximity)]
        public Composite DefiasReaper() { return ScriptHelpers.CreateClearArea(()=>new WoWPoint(-209.3573,-567.5178,20.97694),50,u=>u.Entry == 47403); }

        [EncounterHandler(0)]
        public Composite RootLogic()
        {
            return
                new PrioritySelector(
                    ScriptHelpers.CreateRunAwayFromBad(ctx => true,20f, 47314) // Sticky Bomb
                    );
        }

        [EncounterHandler(47162, "Glubtok")]
        public Composite GlubtokFight()
        {
            
            return
                new PrioritySelector(
                            ScriptHelpers.CreateTankFaceAwayGroupUnit(10)
                                    );
        }


        private WoWGameObject Cannon
        {
            get
            {
                return ObjectManager.
                    GetObjectsOfType<
                        WoWGameObject>().
                    FirstOrDefault(
                        x =>
                        x.
                            Entry == 16398);
            }
        }


        private WoWGameObject Door
        {
            get
            {
                return ObjectManager.
                    GetObjectsOfType<
                        WoWGameObject>().
                    FirstOrDefault(
                        x =>
                        x.
                            Entry == 16397);
            }
        }

        [ObjectHandler(16398, "Defias Cannon", ObjectRange = 30)]
        public Composite DefiasCannonHandler()
        {

            //WoWGameObject Cannon = ObjectManager.GetObjectsOfType<WoWGameObject>().FirstOrDefault(x => x.Entry == 16398);
            //WoWGameObject Door = ObjectManager.GetObjectsOfType<WoWGameObject>().FirstOrDefault(x => x.Entry == 16397);
            
            return new Decorator(r=>Cannon != null && Door != null && StyxWoW.Me.IsTank() && !StyxWoW.Me.Combat && Cannon.State == WoWGameObjectState.Ready && Door.State != WoWGameObjectState.ActiveAlternative,
      new Action(z=>Cannon.Interact()));
                }


        [EncounterHandler(43778, "Foe Reaper 5000")]
        public Composite FoeReaperFight()
        {
            WoWUnit bestTarget = null;
            return new PrioritySelector(
                    new PrioritySelector(ctx => bestTarget = ScriptHelpers.FindBestTargetWithIdsRange(40, 43778),
                            ScriptHelpers.CreateRunAwayFromBad(ctx => bestTarget != null && (bestTarget.CastingSpellId == 88481 || bestTarget.CastingSpellId == 88501) && StyxWoW.Me.IsFollower(),10f, 43778)),
                    
                    ScriptHelpers.CreateTankFaceAwayGroupUnit(10));
        }
        [EncounterHandler(47626, "Admiral Ripsnarl")]
        public Composite AdmiralRipsnarlFight()
        {
            return
                new PrioritySelector(
                                new Decorator(ret => StyxWoW.Me.Location.Distance(new WoWPoint(-61.49663, -820.5336, 41.42817)) > 15,
                                    new PrioritySelector(
                                        new Action(ctx => Navigator.MoveTo(new WoWPoint(-61.49663, -820.5336, 41.42817))))),
                                ScriptHelpers.CreateTankFaceAwayGroupUnit(10)
                                    );
        }
        //good 48006 48296  // bad 48276 
        [EncounterHandler(47739, "\"Captain\" Cookie")]
        public Composite CaptainCookieFight()
        {
            return
                new PrioritySelector(
                                new Decorator(ret => ScriptHelpers.Tank == StyxWoW.Me,
                                    new PrioritySelector(
                                         new Decorator(ret => ScriptHelpers.FindBestTargetWithIdsRange(40, 48006).IsWithinMeleeRange,
                                                 new Action(ret => ScriptHelpers.FindBestTargetWithIdsRange(40, 48006).Interact())),
                                         new Decorator(ret => !ScriptHelpers.FindBestTargetWithIdsRange(40, 48006).IsWithinMeleeRange,
                                                 new Action(ret => Navigator.MoveTo(ScriptHelpers.FindBestTargetWithIdsRange(40, 48006).Location))),
                                         new Decorator(ret => ScriptHelpers.FindBestTargetWithIdsRange(40, 48296).IsWithinMeleeRange,
                                                 new Action(ret => ScriptHelpers.FindBestTargetWithIdsRange(40, 48296).Interact())),
                                         new Decorator(ret => !ScriptHelpers.FindBestTargetWithIdsRange(40, 48296).IsWithinMeleeRange,
                                                 new Action(ret => Navigator.MoveTo(ScriptHelpers.FindBestTargetWithIdsRange(40, 48296).Location))),
                                         new Decorator(ret => ScriptHelpers.FindBestTargetWithIdsRange(40, 48276).IsWithinMeleeRange,
                                                 new Action(ret => ScriptHelpers.FindBestTargetWithIdsRange(40, 48276).Interact())),
                                         new Decorator(ret => !ScriptHelpers.FindBestTargetWithIdsRange(40, 48276).IsWithinMeleeRange,
                                                 new Action(ret => Navigator.MoveTo(ScriptHelpers.FindBestTargetWithIdsRange(40, 48276).Location))
                                             
                                             )))
                                    );
        }


        #endregion
    }
}
